package nimy.info.android.dinnertime.page;

import java.util.Collection;
import java.util.LinkedList;

/**
 * ArrayList does not allow duplicated and null elements
 * 
 * @author 234aini@gmail.com 2012-10-31
 * 
 * @param <E>
 */
public class NoDuplicatedArrayList<E> extends LinkedList<E> {

	private static final long serialVersionUID = 1354101812699116541L;

	public NoDuplicatedArrayList(int size) {
	}

	/**
	 * It always add to the start of the list. More like a Stack
	 */
	@Override
	public boolean add(E object) {
		if (object == null) {
			return false;
		}
		if (contains(object)) {
			return false;
		}
		super.addFirst(object);
		return true;
	}

	@Override
	public boolean addAll(Collection<? extends E> collection) {
		if (collection == null || collection.isEmpty()) {
			return false;
		} else {
			LinkedList<E> temp = new LinkedList<E>();
			for (E item : collection) {
				if(!contains(item)){
					temp.addLast(item);
				}
			}
			return addAll(0, temp);
		}
	}
}
